/*
 * @Author: Orangepi - xcy2001@sina.cn
 * @Date: 2022-01-01 18:01:51
 * @LastEditTime: 2022-01-01 20:52:26
 * @LastEditors: Orangepi - xcy2001@sina.cn
 * @Description: 请求封装
 * @FilePath: \Uni-appTest\test\src\network\request.js
 */
// let baseUrl = "http://127.0.0.1:3000/api"
let baseUrl = "https://qc6d85.app.cloudendpoint.cn/api"
function service(options = {}) {
  options.url = baseUrl + options.url;
  options.header = {
    'content-type': 'application/x-www-form-urlencoded'
  };
  if (options.method === "POST") {
    //配置请求头
    options.header['content-type'] = 'application/json;charset=UTF-8';
    console.log(options);
    for (var key in options.data) {
      if (options.data[key] === '') {
        delete options.data[key]
      }
    }
    options.data = JSON.stringify(options.data)
  }
  const token = uni.getStorageSync("jwt");
  if (token) {
    console.log("已经添加jwt");
    options.header['Authorization'] = token;
  }
  return new Promise((resolved, rejected) => {
    //成功
    options.success = (res) => {
      console.log(res);
      if (parseInt(res.data.status)=== 200) {  //请求成功
        uni.showToast({
          icon: 'none',
          duration: 3000,
          title: `${res.data.msg}`
        });
        resolved(res.data);
      } else {
        uni.showToast({
          icon: 'none',
          duration: 3000,
          title: `${res.data.msg}`
        });
        resolved(res.data.msg);//错误
      }

    }
    //错误
    options.fail = (err) => {
      rejected(err); //错误
    }
    uni.request(options);

  });
}
export default service;
